﻿using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.Web.Pages.SMD.Model;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace H2.NET.Web.Pages.SMD.Service
{
    public static class DeviceService
    {
        public static bool IsExistSn(string sn)
        {
            if (string.IsNullOrEmpty(sn))
            {
                return false;
            }

            return (t_smd_Device._EapproveSN == sn).Count<t_smd_Device>(Storage.Context3) > 0;
        }
        public static bool IsExistSnFullSetSn(string fullSetSn)
        {
            if (string.IsNullOrEmpty(fullSetSn))
            {
                return false;
            }

            return (t_smd_Device._FullSetSN == fullSetSn).Count<t_smd_Device>(Storage.Context3) > 0;
        }

        public static t_smd_Device GetSmdDeviceById(int deviceid)
        {
            var device = (t_smd_Device._DeviceID == deviceid).ListFirst<t_smd_Device>(Storage.Context3);
            return device ?? new t_smd_Device();
        }
        public static t_smd_Device GetSmdDeviceByFullSetSN(string fullSetSN)
        {
            var device = (t_smd_Device._FullSetSN == fullSetSN).ListFirst<t_smd_Device>(Storage.Context3);
            return device ?? new t_smd_Device();
        }

        /// <summary>
        /// 获取门店有效设备
        /// </summary>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public static List<t_smd_Device> FindEnabledDevice(int storeid)
        {
            return (t_smd_Device._StoreID == storeid & t_smd_Device._Status == "使用").List<t_smd_Device>(Storage.Context3).ToList();
        }

        /// <summary>
        /// 获取正常（非报废）设备
        /// </summary>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public static List<t_smd_Device> FindNormalDevice(int storeid)
        {
            return (t_smd_Device._StoreID == storeid & t_smd_Device._Status != "报废").List<t_smd_Device>(Storage.Context3).ToList();
        }

        public static List<t_smd_Device> FindByStoreId(int storeid)
        {
            return (t_smd_Device._StoreID == storeid).List<t_smd_Device>(Storage.Context3).ToList();
        }

        public static t_smd_Device GetOneByStoreId(int storeid)
        {
            return (t_smd_Device._StoreID == storeid).ListFirst<t_smd_Device>(Storage.Context3);
        }
        public static List<t_smd_Device> FindByDealerId(int dealerid)
        {
            var stores = (t_smd_Store._DealerID == dealerid).List<t_smd_Store>(Storage.Context3);
            var storeids = stores.Select(t => t.StoreID);
            return (t_smd_Device._StoreID.In(storeids)).List<t_smd_Device>(Storage.Context3).ToList();
        }

        public static List<t_smd_Device> FindByDeviceIds(List<int> deviceIds)
        {
            return t_smd_Device._DeviceID.In(deviceIds).List<t_smd_Device>(Storage.Context3).ToList();
        }

        public static v_DeviceDeposit GetDeviceDepositById(int deviceid)
        {
            var device = (v_DeviceDeposit._DeviceID == deviceid).ListFirst<v_DeviceDeposit>(Storage.Context3);
            return device;
        }
        /// <summary>
        /// 获取门店有效设备
        /// </summary>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public static List<v_DeviceDeposit> FindEnabledDeviceDeposit(int storeid)
        {
            return (v_DeviceDeposit._StoreID == storeid & v_DeviceDeposit._Status == "使用").List<v_DeviceDeposit>(Storage.Context3).ToList();
        }
        /// <summary>
        /// 获取正常（非报废）设备
        /// </summary>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public static List<v_DeviceDeposit> FindNormalDeviceDeposit(int storeid)
        {
            return (v_DeviceDeposit._StoreID == storeid & v_DeviceDeposit._Status != "报废").List<v_DeviceDeposit>(Storage.Context3).ToList();
        }

        /// <summary>
        /// 根据经销商获取设备
        /// </summary>
        /// <param name="storeid"></param>
        /// <returns></returns>
        public static List<v_DeviceDeposit> FindNormalDeviceDepositByDealerId(int dealerId)
        {
            var stores = (t_smd_Store._DealerID == dealerId).List<t_smd_Store>(Storage.Context3);

            var storeids = stores.Select(t => t.StoreID);
            if (storeids != null && storeids.Any())
                return (v_DeviceDeposit._StoreID.In(storeids) & v_DeviceDeposit._Status != "报废").List<v_DeviceDeposit>(Storage.Context3).ToList();
            else
                return new List<v_DeviceDeposit>();
        }

        public static List<v_DeviceDeposit> FindNormalDeviceDepositByStoreIds(List<int> storeIds)
        {
            if (storeIds != null && storeIds.Any())
                return (v_DeviceDeposit._StoreID.In(storeIds) & v_DeviceDeposit._Status != "报废").List<v_DeviceDeposit>(Storage.Context3).ToList();
            else
                return new List<v_DeviceDeposit>();
        }

        public static bool IsUnfinish(List<int> devices) 
        {
            if (devices == null || devices.Count == 0)
            {
                return false;
            }

            var str_deviceid = string.Join(",", devices);

            var sqlbuilder = new StringBuilder();
            sqlbuilder.AppendLine("select count(*) dCount from v_DeviceUnfinish");
            sqlbuilder.AppendLine(" where deviceid in (" + str_deviceid + ")");

            DBUtility.DbHelperSQL.connectionString = DBUtility.PubConstant.GetConnectionString("oleDB");
            var dt_result = DBUtility.DbHelperSQL.Query(sqlbuilder.ToString()).Tables[0];

            return Utils.ObjToInt(dt_result.Rows[0].ItemArray[0],0) > 0;
        }
    }
}